package com.zbbz.advice;

import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @author WangYang
 * @date 2025/02/13/14:50
 */
@ControllerAdvice
public class ValidException {


    // Json验证器验证失败拦截
    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public AjaxResult handler(MethodArgumentNotValidException e){
        // 获取验证错误字段名称和错误信息
        String error = e.getBindingResult().getFieldError().getDefaultMessage();
        return AjaxResult.error(error);
    }
}
